#include "management.h"

Management::Management()
{
    qDebug()<<"管理模块初始化中";
    handlemanage=QSharedPointer<handle_manage>(new handle_manage());
    handle_thread=QSharedPointer<QThread>(new QThread());
    handlemanage.data()->moveToThread(handle_thread.data());
    handle_thread.data()->start();
}

Management::~Management()
{
    qDebug()<<"Management析构";
    handlemanage.clear();
    handle_thread.data()->quit();
    handle_thread.data()->wait();
    handle_thread.clear();
    l_server->deleteLater();
    l_server_thread->quit();
    l_server_thread->wait();
    l_server_thread->deleteLater();
    t_server->deleteLater();
    t_server_thread->quit();
    t_server_thread->wait();
    t_server_thread->deleteLater();
    process.clear();
}

void Management::localServer_init(QString listenName)
{
    qDebug()<<"本地Server模块初始化";
    this->localServer_name=listenName;
    l_server=new local_server(localServer_name);
    l_server->set_handle(QWeakPointer<handle_manage>(handlemanage));
}

bool Management::localServer_start()
{
    if(l_server!=NULL)
    {
        qDebug()<<"开启本地Server模块";
        bool result=l_server->start_server();
        l_server_thread=new QThread();
        l_server->moveToThread(l_server_thread);
        l_server_thread->start();
        return result;
    }
    else
    {
        qDebug()<<"请初始化localserver!";
        return false;
    }
}

void Management::tcpServer_init(int threadCount, int Port)
{
    qDebug()<<"TcpServer模块初始化";
    if(threadCount>100)
    {
        qDebug()<<"tcp服务器设置线程数太多,可能造成程序崩溃!";
    }
    this->tcpServer_ThreadCount=threadCount;
    if(Port<0)
    {
        qDebug()<<"端口设置错误";
        return;
    }
    this->tcpServer_port=Port;
    t_server=new tcp_server(tcpServer_ThreadCount,tcpServer_port);
    t_server->set_Handle(QWeakPointer<handle_manage>(handlemanage));
}

bool Management::tcpServer_start()
{
    qDebug()<<"开启TcpServer模块";
    bool result= t_server->Start_Server();
    t_server_thread=new QThread();
    t_server->moveToThread(t_server_thread);
    t_server_thread->start();
    return result;
}

void Management::process_init()
{
    qDebug()<<"进程模块初始化";
    process=QSharedPointer<process_exe>(new process_exe());
    connect(l_server,SIGNAL(restart_localcaculate()),process.data(),SLOT(local_restart()));
}

bool Management::process_start()
{
    qDebug()<<"启动进程模块";
    process.data()->start_process();
    return true;
}

void Management::Handle_Setcount(int threadcount)
{
    qDebug()<<"处理模块初始化";
    this->handlemanage.data()->setHandleCount(threadcount);
}

void Management::Handle_Start()
{
    qDebug()<<"启动处理模块";
    this->handlemanage.data()->init();
}

void Management::mysql_set(QString SQL_ip, QString SQL_Sname, QString Sql_Username, QString SQL_Password)
{
    qDebug()<<"数据库模块初始化";
    Common::SQL_Lock.lockForWrite();
    Common::mysql_data={SQL_ip,SQL_Sname,Sql_Username,SQL_Password};
    Common::SQL_Lock.unlock();
}

void Management::Log_init(QString logname)
{
    qDebug()<<"日志模块初始化中";
    mylog = QSharedPointer<Log>(new Log());
    mylog.data()->init(logname);
}

void Management::StartLog()
{
    qDebug()<<"启动日志模块";
    mylog.data()->StartLog();
}

void Management::Configur_init(QString Filename)
{
    qDebug()<<"配置模块初始化";
    mysetting = QSharedPointer<Setting>(new Setting());
    mysetting.data()->init(Filename);
}

void Management::StartConfigur()
{
    qDebug()<<"启动配置模块";
    mysetting.data()->Start();
}
